package com.ssrs.framework.schedule;

import com.ssrs.framework.FrameworkPlugin;
import com.ssrs.framework.cache.CacheDataProvider;
import com.ssrs.framework.cache.CacheService;
import com.ssrs.framework.cache.FrameworkCacheManager;
import com.ssrs.framework.extend.annotation.ExtendItem;

/**
 * 定时任务缓存，当修改定时任务时存入缓存来实现cron表达式更新
 *
 * @author ssrs
 */
@ExtendItem(
        plugin = FrameworkPlugin.class,
        extendService = CacheService.class,
        description = "定时任务缓存扩展项"
)
public class SystemTaskCache extends CacheDataProvider {
    private static final String PROVIDER_ID = "SystemTaskCache";
    private static final String TYPE = "SystemTask";

    @Override
    public void onKeyNotFound(String type, String key) {
        SystemTask task = SystemTaskService.getInstance().get(key);
        set(task);
    }

    @Override
    public void onTypeNotFound(String type) {

    }

    @Override
    public String getExtendItemId() {
        return PROVIDER_ID;
    }

    @Override
    public String getExtendItemName() {
        return "定时任务缓存提供类";
    }

    /**
     * 获取缓存中的定时任务
     *
     * @param taskId 任务id
     * @return
     */
    public static SystemTask get(String taskId) {
        return (SystemTask) FrameworkCacheManager.get(PROVIDER_ID, TYPE, taskId);
    }

    public static void set(SystemTask task) {
        if (task == null) {
            return;
        }
        FrameworkCacheManager.set(PROVIDER_ID, TYPE, task.getExtendItemId(), task);
    }

    public static void remove(SystemTask task) {
        if (task == null) {
            return;
        }
        FrameworkCacheManager.remove(PROVIDER_ID, TYPE, task.getExtendItemId());
    }

}
